package demo.xlj.yuyi.studentapp.dudaoyuan;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.common.CommunityStyleActivity;
import demo.xlj.yuyi.studentapp.common.FuZhongStyleActivity;
import demo.xlj.yuyi.studentapp.common.LookAnnouncementActivity;
import demo.xlj.yuyi.studentapp.common.LookMyTongZhiActivity;
import demo.xlj.yuyi.studentapp.common.MyCommicationActivity;
import demo.xlj.yuyi.studentapp.common.SendTongZhiChooseActivity;
import demo.xlj.yuyi.studentapp.common.SocialActivity;
import demo.xlj.yuyi.studentapp.common.StudentStyleActivity;
import demo.xlj.yuyi.studentapp.common.WhereSchoolActivity;
import demo.xlj.yuyi.studentapp.common.YouthLeagueNewsActivity;
import demo.xlj.yuyi.studentapp.login.StudentMeetLoginActivity;
import demo.xlj.yuyi.studentapp.shetuanshencha.AllowOrNotHuoDongActivity;
import demo.xlj.yuyi.studentapp.utils.ActivityUtil;
import demo.xlj.yuyi.studentapp.utils.SharePerferencesUtils;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;

/**
 * 作者：项路俊 on 2018/4/2 0002
 * 邮箱：ThanksForCode@gmail.com
 * 介绍：督导员身份新的界面
 */

public class DuDaoActivity extends BaseRxActivity {


    @BindView(R.id.ImageView_Du_dao_YUan_New_CeHua)
    ImageView mImageViewDuDaoYUanNewCeHua;
    @BindView(R.id.LinearLayout_Du_Dao_One)
    LinearLayout mLinearLayoutDuDaoOne;
    @BindView(R.id.LinearLayout_Du_Dao_Two)
    LinearLayout mLinearLayoutDuDaoTwo;
    @BindView(R.id.LinearLayout_Du_Dao_Three)
    LinearLayout mLinearLayoutDuDaoThree;
    @BindView(R.id.LinearLayout_Du_Dao_Four)
    LinearLayout mLinearLayoutDuDaoFour;
    @BindView(R.id.LinearLayout_Du_Dao_Five)
    LinearLayout mLinearLayoutDuDaoFive;
    @BindView(R.id.LinearLayout_Du_Dao_Six)
    LinearLayout mLinearLayoutDuDaoSix;
    @BindView(R.id.LinearLayout_Du_Dao_Seven)
    LinearLayout mLinearLayoutDuDaoSeven;
    @BindView(R.id.LinearLayout_Du_Dao_Eight)
    LinearLayout mLinearLayoutDuDaoEight;
    @BindView(R.id.LinearLayout_Du_Dao_Ten)
    LinearLayout mLinearLayoutDuDaoTen;
    @BindView(R.id.LinearLayout_Du_Dao_Nine)
    LinearLayout mLinearLayoutDuDaoNine;
    @BindView(R.id.LinearLayout_Du_Dao_Eleven)
    LinearLayout mLinearLayoutDuDaoEleven;
    @BindView(R.id.LinearLayout_Du_Dao_tel)
    LinearLayout mLinearLayoutDuDaoTel;
    @BindView(R.id.NavigationView_Du_Dao)
    NavigationView mNavigationViewDuDao;
    @BindView(R.id.ImageSwitcher_Du_Dao_New)
    ImageSwitcher mImageSwitcherDuDaoNew;
    @BindView(R.id.Drawer_Layout_Du_Dao)
    DrawerLayout mDrawerLayoutDuDao;

    private int[] images = {R.drawable.lunbo4, R.drawable.lunbo5, R.drawable.lunbo6, R.drawable.lunbo7};//图片资源数组
    private int position = 0;


    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 11:
                    position++;
                    mImageSwitcherDuDaoNew.setImageResource(images[position]);
                    if (position == 3) {
                        position = -1;
                    }
                    myHandler.sendEmptyMessageDelayed(11, 4000);
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.du_dao_activity);
        ButterKnife.bind(this);
        String name = (String) SharePerferencesUtils.get(DuDaoActivity.this, "name", "", 0);
        ToastUtil.success(DuDaoActivity.this, "欢迎" + name + "归来！", true, true);
        initImageView();
        View view = mNavigationViewDuDao.getHeaderView(0);

        TextView textView_Name = (TextView) view.findViewById(R.id.TextView_Chou_Ti_Name);
        TextView textView_ShenFen = (TextView) view.findViewById(R.id.TextView_Chou_Ti_Shen_Fen);

        textView_Name.setText("用户姓名："+name);
        textView_ShenFen.setText("用户身份：督导员");

        initListener();
    }

    private void initListener() {
        if (mNavigationViewDuDao != null) {
            mNavigationViewDuDao.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id = item.getItemId();

                    if (id == R.id.nav_camera) {
                        //表示退出登录
                        ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, StudentMeetLoginActivity.class);
                        DuDaoActivity.this.finish();
                    } else if (id == R.id.nav_gallery) {
                        //表示我收藏的社团
                        ActivityUtil.openActivityFromLeftToRightByResult(DuDaoActivity.this, CommunityStyleActivity.class, "type", "1");
                    } else if (id == R.id.nav_slideshow) {
                        //表示我的发布
                        ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, LookMyTongZhiActivity.class);
                    }

                    return true;
                }
            });
        }

        if (mImageViewDuDaoYUanNewCeHua != null) {
            mImageViewDuDaoYUanNewCeHua.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                      mDrawerLayoutDuDao.openDrawer(GravityCompat.START);
                }
            });
        }
    }


    //初始化，上面的轮播图
    private void initImageView() {
        mImageSwitcherDuDaoNew.setFactory(new ViewSwitcher.ViewFactory() {//设定工厂，每进来一个图片都用一个ImageView接收
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(DuDaoActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                return imageView;
            }
        });//这个设置Factory是必须的
        mImageSwitcherDuDaoNew.setImageResource(images[position]);//设置初始图片,也是通过这个代码设置在控件上显示的图片
        mImageSwitcherDuDaoNew.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.lunbo_in));
        mImageSwitcherDuDaoNew.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.lunbo_out));//动画淡出

        myHandler.sendEmptyMessageDelayed(11, 3000);

        mImageSwitcherDuDaoNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击事件
                ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, WhereSchoolActivity.class);
            }
        });
    }

    @OnClick({R.id.LinearLayout_Du_Dao_One, R.id.LinearLayout_Du_Dao_Two, R.id.LinearLayout_Du_Dao_Three, R.id.LinearLayout_Du_Dao_Four, R.id.LinearLayout_Du_Dao_Five, R.id.LinearLayout_Du_Dao_Six, R.id.LinearLayout_Du_Dao_Seven, R.id.LinearLayout_Du_Dao_Eight, R.id.LinearLayout_Du_Dao_Ten, R.id.LinearLayout_Du_Dao_Nine, R.id.LinearLayout_Du_Dao_Eleven, R.id.LinearLayout_Du_Dao_tel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.LinearLayout_Du_Dao_One:
                ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, FuZhongStyleActivity.class);//附中风采
                break;
            case R.id.LinearLayout_Du_Dao_Two:
                ActivityUtil.openActivityFromLeftToRightByResult(DuDaoActivity.this, CommunityStyleActivity.class, "type", "0");//社团风貌
                break;
            case R.id.LinearLayout_Du_Dao_Three:
                ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, YouthLeagueNewsActivity.class);//附中要闻
                break;
            case R.id.LinearLayout_Du_Dao_Four:
                ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, SocialActivity.class);//社团活动
                break;
            case R.id.LinearLayout_Du_Dao_Five:
                ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, StudentStyleActivity.class);//学生会风采
                break;
            case R.id.LinearLayout_Du_Dao_Six:
                ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, LookAnnouncementActivity.class);//公告通知
                break;
            case R.id.LinearLayout_Du_Dao_Seven:
                ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, SendTongZhiChooseActivity.class);//发布通知
                break;
            case R.id.LinearLayout_Du_Dao_Eight:
                ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, AllowOrNotHuoDongActivity.class);//待审核
                break;
            case R.id.LinearLayout_Du_Dao_Ten:
                ToastUtil.info(DuDaoActivity.this, "暂没有更多数据", true, true);
                break;
            case R.id.LinearLayout_Du_Dao_Nine:
                ActivityUtil.openActivityForResultFromLeftToRight(DuDaoActivity.this, MyCommicationActivity.class, 99);//我的
                break;
            case R.id.LinearLayout_Du_Dao_Eleven:
                //跳到附中要闻
                ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, YouthLeagueNewsActivity.class);//附中要闻
                break;
            case R.id.LinearLayout_Du_Dao_tel:
                //跳到社团活动
                ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, SocialActivity.class);//社团活动
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99 && resultCode == 88) {
            ActivityUtil.openActivityFromLeftToRight(DuDaoActivity.this, StudentMeetLoginActivity.class);
            DuDaoActivity.this.finish();
        }
    }


    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {

            if ((System.currentTimeMillis() - exitTime) > 2000)  //System.currentTimeMillis()无论何时调用，肯定大于2000
            {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
